package main

import (
	"encoding/json"
	"fmt"
	"go.manyji.com/lib/util/convert"
)

func GetActivities(deviceId string) ([]*ActivityData, error) {

	repMap := map[string]interface{}{}
	actData, err := getAcitityList(deviceId)
	if err != nil {
		return nil, err
	}

	err = json.Unmarshal([]byte(actData), &repMap)
	if err != nil {
		return nil, err
	}

	dataMaps, ok := repMap["Data"].(map[string]interface{})["act"].([]interface{})
	if !ok {
		return nil, fmt.Errorf("%v", repMap["Code"])
	}

	var acts []*ActivityData

	for _, dataMap := range dataMaps {
		u := dataMap.(map[string]interface{})

		if u["id"] == 0 {
			continue
		}

		acts = append(acts, &ActivityData{
			Id:           int64(convert.Atoi(convert.ToString(u["id"]))),
			Price:        int64(convert.Atoi(convert.ToString(u["price"]))),
			Coins:        int64(convert.Atoi(convert.ToString(u["coins"]))),
			Bonus:        int64(convert.Atoi(convert.ToString(u["bonus"]))),
			BonusExpired: convert.Atoi(convert.ToString(u["bonus_expired"])),
		})
	}
	return acts, nil
}
